package cn.alex.sso.project.controller;

import cn.alex.sso.framework.annotations.OperatLog;
import cn.alex.sso.framework.constant.ActionConstant;
import cn.alex.sso.framework.constant.ModuleConstant;
import cn.alex.sso.framework.core.Result;
import cn.alex.sso.project.entity.SysLoginLog;
import cn.alex.sso.project.service.SysLoginLogService;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录日志
 *
 * @author Alex
 * @date 2024/11/28 17:51
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/logs/login")
public class SysLoginLogController {
    private final SysLoginLogService baseService;

    /**
     * 获取分页列表
     */
    @GetMapping("/getPage")
    public Result getPage(SysLoginLog entity) {
        return Result.ok(baseService.getPage(entity));
    }

    /**
     * 获取当前登录用户的分页列表
     */
    @GetMapping("/getPageByCurrentLogin")
    public Result getPageByCurrentLogin(SysLoginLog entity) {
        entity.setUserId(StpUtil.getLoginIdAsLong());
        return Result.ok(baseService.getPage(entity));
    }

    /**
     * 删除
     */
    @DeleteMapping
    @OperatLog(module = ModuleConstant.LOGIN_LOG, action = ActionConstant.REMOVE)
    public Result del(@RequestBody List<Long> ids) {
        baseService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 清空
     */
    @DeleteMapping("/clear")
    @OperatLog(module = ModuleConstant.LOGIN_LOG, action = ActionConstant.CLEAR_DATA)
    public Result clear() {
        baseService.remove(new LambdaQueryWrapper<SysLoginLog>()
                .ne(SysLoginLog::getId, -1)
        );
        return Result.ok();
    }
}
